<?php
// +----------------------------------------------------------------------
// | Name 风音框架
// +----------------------------------------------------------------------
// | Author 唐启云 <tqy@fxri.net>
// +----------------------------------------------------------------------
// | Copyright Copyright © 2016-2099 方弦研究所. All rights reserved.
// +----------------------------------------------------------------------
// | Link https://www.fxri.net
// +----------------------------------------------------------------------
namespace fxapp\facade_ext;

/**
 * 用户类
 */
class User
{
    /**
     * 解析数据-密码
     * @param string $password 密码
     * @param string $confirm_password 确认密码
     * @param string $type 账户类型
     * @return mixed
     */
    public function password($password = null, $confirm_password = null, $type = null)
    {
        // 初始化变量
        $echo = \fxapp\Server::echo();
        switch ($type) {
            default:
                // 默认
                $wind['pw_name'] = 'password';
                $wind['pw_start'] = 8;
                $wind['pw_end'] = 64;
                break;
            case 1:
                // 密码锁
                // $wind['pw_name'] = 'password';
                // $wind['pw_start'] = 6;
                // $wind['pw_end'] = 12;
                $wind['pw_name'] = 'password';
                $wind['pw_start'] = 8;
                $wind['pw_end'] = 64;
                break;
            case 2:
                // 密码锁
                // $wind['pw_name'] = 'code_lock';
                // $wind['pw_start'] = 6;
                // $wind['pw_end'] = 6;
                $wind['pw_name'] = 'code_lock';
                $wind['pw_start'] = 8;
                $wind['pw_end'] = 64;
                break;
        }
        $record = $this->passwordStyle($password, $confirm_password, $wind['pw_start'], $wind['pw_end'], $wind['pw_name']);
        if (!$record[0]) {
            return $record;
        } else if (!$this->passwordPower($password, $type)) {
            $echo[0] = false;
            $echo[1] = 1004;
            $echo[2] = \fxapp\Base::lang([$wind['pw_name'], 'not', 'safe', ',', 'need', 'at least', '[', 'number2', ',2', 'english2', ']']);
            return $echo;
        }
        return $echo;
    }

    /**
     * 解析数据-密码强度
     * @param string $password 密码
     * @param string $type 账户类型
     * @return mixed
     */
    public function passwordPower($password, $type = null)
    {
        // 初始化变量
        $score = 0;
        $success = false;
        switch ($type) {
            default:
            case 1:
                // 默认
                if (preg_match('/[0-9]+/', $password)) {
                    ++$score;
                }
                if (preg_match('/[a-z]+/', $password)) {
                    ++$score;
                }
                if (preg_match('/[A-Z]+/', $password)) {
                    ++$score;
                }
                if (preg_match('/[^0-9a-zA-Z]+/', $password)) {
                    ++$score;
                }
                if ($score >= 2) {
                    $success = true;
                }
                break;
            case 2:
                // 密码锁
                if (preg_match('/[0-9]+/', $password)) {
                    ++$score;
                }
                if (preg_match('/[^0-9]+/', $password)) {
                    --$score;
                }
                if ($score >= 1) {
                    $success = true;
                }
                break;
        }
        return $success;
    }

    /**
     * 解析数据-密码样式
     * @param string $password 密码
     * @param string $confirm_password 确认密码
     * @param string $pass_min 密码下位
     * @param string $pass_max 密码上位
     * @param string $pass_name 密码名称
     * @return mixed
     */
    public function passwordStyle($password = null, $confirm_password = null, $pass_min = 6, $pass_max = 12, $pass_name = 'password')
    {
        // 初始化变量
        $echo = \fxapp\Server::echo();
        if (!\fxapp\Data::paramEmpty([$password])[0]) {
            $echo[0] = false;
            $echo[1] = 1000;
            $echo[2] = \fxapp\Base::lang(['lack', $pass_name]);
            return $echo;
        } else if (\fxapp\Data::paramEmpty([$confirm_password])[0] && $confirm_password !== $password) {
            $echo[0] = false;
            $echo[1] = 1001;
            $echo[2] = \fxapp\Base::lang(['confirm_' . $pass_name, 'not', 'same']);
            return $echo;
        }
        $data = \fxapp\Text::strlen($password, $pass_min, $pass_max);
        if (!$data[0]) {
            $echo[0] = false;
            $echo[1] = 1001;
            $echo[2] = \fxapp\Base::lang([$pass_name, $data[1]]);
            return $echo;
        }
        return $echo;
    }

    /**
     * 解析数据-令牌
     * @param string $var 变量
     * @param integer $mode 模式
     * @return mixed
     */
    public function token($var, $mode = null)
    {
        // 初始化变量
        $token = \fxapp\Safe::token($var, 'decode');
        if (false === $token) return $token;
        $token = explode(',', $token);
        switch ($mode) {
            default:
                // 默认
                $data = $token;
                break;
            case 1:
                // 账号
                if (count($token) != 4) {
                    // 参数数量不对
                    return false;
                }
                $param['time'] = $token[0];
                $param['type'] = $token[1];
                $param['id'] = $token[2];
                $param['source'] = $token[3];
                $param['token'] = $var;
                $data = $param;
                break;
        }
        return $data;
    }

    /**
     * 解析数据-来源
     * @return mixed
     */
    public function source()
    {
        // 检查来源
        switch (\fxapp\Client::system()) {
            default:
                // 其他系统
                $data = 1;
                break;
            case 'iOS':
                // 苹果系统
                $data = 2;
                break;
            case 'Android':
                // 安卓
                $data = 3;
                break;
        }
        return $data;
    }
}
